package org.bigdata.framework.core;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.RandomStringUtils;
import org.bigdata.framework.core.utils.MD5;
import org.bigdata.framework.core.utils.RandomUtil;
import org.bigdata.framework.core.utils.SignUtil;
import org.bigdata.framework.core.utils.XMLUtil;
import org.bigdata.framework.core.utils.XmlPostUtil;

public class App {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws Exception {
		
		String nonce_str = RandomStringUtils.random(32, true, true);

    

    	System.out.println(RandomUtil.createRandomString(32));
    	SortedMap<Object, Object> paramMap = new TreeMap<Object, Object>();
        paramMap.put("appid","wxafc0a7b353f064a2");//公众号ID
        paramMap.put("mch_id","1572010711");//商户号
        paramMap.put("nonce_str",RandomUtil.createRandomString(32));//32位随机字符串
        //paramMap.put("body",URLEncoder.encode("简学-订单支付", "UTF-8"));//商品描述
        paramMap.put("body","简学-订单支付");//商品描述
        paramMap.put("out_trade_no","20190812255347");//商户订单号
        paramMap.put("total_fee","1");//设置交易金额 金额为分
        paramMap.put("spbill_create_ip","115.171.167.172");//客户机IP
        paramMap.put("notify_url","http://www.weixin.qq.com/wxpay/pay.php");//通知地址
        paramMap.put("trade_type","APP");//支付方式 原生扫码
        //paramMap.put("sign_type","MD5");//支付方式 原生扫码
       //paramMap.put("sign_type","HMAC-SHA256");//支付方式 原生扫码
        
        //paramMap.put("openid", openid);//添加openid
        paramMap.put("sign",SignUtil.createSign(paramMap,"f9f6c81fdbab50147d6f2c4fcee60aa5"));
        //paramMap.put("sign",SignUtil.createSignHMACSHA256(paramMap,"f9f6c81fdbab50147d6f2c4fcee60aa5"));
       for (Object string : paramMap.keySet()) {
		System.out.println(string);
	}
        String rXml = "";
        String return_code="";
        try {
        	String mapToXml = XMLUtil.mapToXml(paramMap);
        	System.out.println(mapToXml);
            rXml = new String(XmlPostUtil.sendXmlRequest("https://api.mch.weixin.qq.com/pay/unifiedorder", mapToXml));
            System.out.println(rXml);
            return_code = (String) XMLUtil.doXMLParse(rXml).get("return_code");//得到预支付id
            System.out.println(return_code);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    	
        long t1=System.currentTimeMillis();
		Date date2=new Date();
		date2.setTime(t1);
    	System.out.println(date2);
    	
    	System.out.println(t1/1000);
	}

}
